package com.vpen.account.demo.service.impl;

import com.vpen.account.demo.model.dto.LoginDto;
import com.vpen.account.demo.service.AccountLoginService;
import com.vpen.account.demo.service.AccountUserService;
import com.vpen.account.demo.utils.CodeUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

/**
 * 手机的登录
 *
 * @author 韦鹏
 * @date 2022/8/16 14:26
 */
@Service("phoneLoginService")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class PhoneLoginServiceImpl implements AccountLoginService {

    private final AccountUserService accountUserService;


    @Override
    public UserDetails getUserByCondition(LoginDto loginDto) {
        // 1.校验短信验证码
        if (!CodeUtils.validCode(loginDto.getPhone(), loginDto.getCode())) {
            throw new BadCredentialsException("验证码错误,请重新确认.");
        }
        return AccountUserDetailServiceImpl.loadUserDetails(accountUserService.selectOneByPhone(loginDto.getPhone()));
    }
}
